<template>
  <div>
    <el-form ref="form" label-width="80px">
      <!-- SKU名称 -->
      <el-form-item label="SPU名称"> {{ spu.spuName }}</el-form-item>
      <!-- SKU名称 -->
      <el-form-item label="SKU名称">
        <el-input placeholder="sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <!-- 价格 -->
      <el-form-item label="价格">
        <el-input v-model="skuInfo.price" type="number"></el-input>
      </el-form-item>
      <!-- 重量 -->
      <el-form-item label="重量(千克)">
        <el-input v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <!-- 规格描述 -->
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <!-- 平台属性 -->
      <el-form-item label="平台属性">
        <el-form ref="form" label-width="80px" inline>
          <el-form-item
            :label="attr.attrName"
            v-for="(attr, index) in attrInfoList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="(attrValue, index) in attr.attrValueList"
                :key="attrValue.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <!-- 销售属性 -->
      <el-form-item label="销售属性">
        <el-form ref="form" label-width="80px" inline>
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="(saleAttr, index) in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                v-for="(saleAttrValue, index) in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <!-- 图片列表 -->
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <!-- 第一列 复选框 -->
          <el-table-column prop="prop" width="50" type="selection">
          </el-table-column>
          <!-- 第二列 图片 -->
          <el-table-column label="图片" width="width">
            <template slot-scope="{ row, $index }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <!-- 第三列 图片名称 -->
          <el-table-column prop="imgName" label="图片名称" width="width">
          </el-table-column>
          <!-- 第四列 操作 -->
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="primary"
                v-if="!row.isDefault"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button type="success" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
export default {
  name: "SkuForm",
  components: {},
  data() {
    return {
      //存储图片的信息
      spuImageList: [],
      //存储销售是属性
      spuSaleAttrList: [],
      //存储平台属性的数据
      attrInfoList: [],
      spu: {},
      skuInfo: {
        //第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：需要通过数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        //第三类：需要自己书写代码
        //默认图片
        skuDefaultImg: "",
        //收集图片的字段
        skuImageList: [],
        //平台属性
        skuAttrValueList: [],
        //销售属性
        skuSaleAttrValueList: [],
      },
      //收集图片的数据字段:但是需要注意，收集的数据目前缺少isDefault字段，将来提交给服务器数据的时候，需要整理参数
      imageList: [],
    };
  },

  methods: {
    async getData(category1Id, category2Id, spu) {
      //收集父组件给予的数据
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spu = spu;
      //获取图片的数据
      let result = await this.$API.spu.reqSpuImageLIst(spu.id);
      if (result.code == 200) {
        console.log(result);
        let list = result.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      //获取销售属性的数据
      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id);
      if (result1.code == 200) {
        let arr = result1.data;
        arr.forEach((item) => {
          this.$set(item, "attrIdAndValueId", "");
        });
        this.spuSaleAttrList = arr;
      }

      //获取平台属性的数据
      let result2 = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result2.code == 200) {
        // 给每一个项都加上attrInfoList属性手机id
        let arr = result2.data;
        arr.forEach((item) => {
          this.$set(item, "attrIdAndValueId", "");
        });
        console.log(arr, "子组件arr");
        this.attrInfoList = arr;
      }
    },

    //取消按钮
    cancel() {
      //自定义事件，让父组件切换场景0
      this.$emit("changeScenes", 1);
      //清除数据
      Object.assign(this._data, this.$options.data());
    },
    //复选框发生改变的时候
    handleSelectionChange(params) {
      //获取到用户选中图片的信息数据，
      this.imageList = params;
    },
    //设置默认按钮
    changeDefault(row) {
      //图片列表的数据的isDefault字段变为 0
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      //点击的那个图片的数据变为1
      row.isDefault = 1;
      //收集默认图片的地址
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    //保存按钮的事件
    async save() {
      //整理参数
      //整理平台属性
      console.log("baocun ");
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this;
      //整理平台属的数据
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);

      //整理销售属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] =
            item.attrIdAndValueId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      //整理图片的数据
      skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuId: item.id,
        };
      });

      console.log(this.skuInfo);
      //发请求
      let result = await this.$API.spu.reqAddSku(skuInfo);

      if (result.code == 200) {
        console.log(result);
        this.$message({ type: "success", message: "添加SKU成功" });
        this.$emit("changeScenes", 0);
      }
    },
  },
};
</script>


<style scoped>
</style>